package com.xtkj.accountjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xtkj.accountjava.entity.AccountBookEntity;
import com.xtkj.accountjava.entity.UserAccountBookEntity;
import com.xtkj.accountjava.mapper.AccountBookMapper;
import com.xtkj.accountjava.mapper.UserAccountBookMapper;
import com.xtkj.accountjava.service.AccountBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AccountBookServiceImpl implements AccountBookService {
    @Autowired
    AccountBookMapper accountBookMapper;
    @Autowired
    UserAccountBookMapper userAccountBookMapper;


    @Override
    public AccountBookEntity findAccountBookAndCategorysById(int id) {
        return accountBookMapper.findAccountBookAndCategorysById(id);
    }

    @Override
    public List<AccountBookEntity> findOnlyAccountBooksByIds(List ids) {
        return accountBookMapper.selectBatchIds(ids);
    }
    @Override
    public AccountBookEntity findOnlyAccountBooksById(int id) {
        return accountBookMapper.selectById(id);
    }

    @Override
    public List<AccountBookEntity> findAllAccountBooksAndCategorys() {
        return accountBookMapper.findAllAccountBooksAndCategorys();
    }

    @Override
    public List<AccountBookEntity> findAllAccountBooksAndCategorysByPage(int currPage, int pageSize) {
        Page<AccountBookEntity> page= new Page<>(currPage,pageSize);
        LambdaQueryWrapper<AccountBookEntity> queryWrapper = new LambdaQueryWrapper<>();
        //是否置顶进行排序 创建日期排序
//        queryWrapper.orderByDesc(AccountBookEntity::getWeight,AccountBookEntity::getCreateDate);
        Page<AccountBookEntity> accountBookEntityPage = accountBookMapper.selectPage(page, queryWrapper);
        List<AccountBookEntity> records = accountBookEntityPage.getRecords();
        return  records;
    }

    @Override
    public List<UserAccountBookEntity> findAllAccountBooksByUserId(int userId) {
        List<UserAccountBookEntity> allAccountBooksByUserIdEntities = userAccountBookMapper.findAllAccountBooksByUserId(userId);
        allAccountBooksByUserIdEntities.forEach(item->{
            Long accountBookId = item.getAccountBookId();
            AccountBookEntity accountBookEntity = accountBookMapper.selectById(accountBookId);
            item.setAccountBookEntity(accountBookEntity);
        });
        return allAccountBooksByUserIdEntities;
    }

    @Override
    public UserAccountBookEntity addUserAccountBook(UserAccountBookEntity userAccountBook) {
        Integer insert = userAccountBookMapper.insert(userAccountBook);
        if(insert==0){
            return  null;
        }else {
            return  userAccountBook;
        }
    }

    @Override
    public UserAccountBookEntity updateUserAccountBook(UserAccountBookEntity userAccountBook) {
        Integer updateById = userAccountBookMapper.updateById(userAccountBook);
        if(updateById==0){
            return  null;
        }
        return userAccountBook;
    }

    @Override
    public UserAccountBookEntity add_default_account_book(int userId) {
            UserAccountBookEntity userAccountBookEntity =new UserAccountBookEntity();
            userAccountBookEntity.setUserId(Long.parseLong(userId+""));
            userAccountBookEntity.setAccountBookId(1L);
            userAccountBookEntity.setCreateTime(new Date());
            userAccountBookEntity.setDescription("");
            userAccountBookEntity.setAccountBookEntity(findOnlyAccountBooksById(1));
            UserAccountBookEntity userAccountBook = addUserAccountBook(userAccountBookEntity);

            return  userAccountBook;
    }

}
